<?php

namespace app\index\controller;
use app\index\model\Base as bs;
use app\index\controller\Paywx;
use app\index\model\User as us;
use app\index\controller\EasyRefund;
class User extends Base
{
    /**
     *用户登录/注册
     * @param
     * @return
     */
    public function userRegister(){
        $open_id = input('open_id');
        if(!$open_id){
            $this->apiReturn(303,'lack open_id');
        }
        $return_key = bs::getKey('user',['open_id'=>$open_id],'id');
        if($return_key){
            $user_info = bs::particular('user','id,authority,add_time,phone,gender,age,expiration,id_card,location,url',$return_key);
            $user_info['add_time'] = date('Y-m-d H:i:s',$user_info['add_time']);
            $ex_time = $user_info['expiration'] - time();
//            halt($ex_time);
            if($ex_time>=86400){
                $user_info['expiration'] = intval($ex_time / 86400);
            }elseif ($ex_time>=0){
                $user_info['expiration'] = round(($ex_time / 3600),2);
            }else{
                $user_info['expiration'] = 0;
            }
            $this->apiReturn(200,'登录成功',$user_info);
        }else{
            $value = I('nickname|gender|url|phone|open_id');
            foreach ($value as $k=>$v){
                if(!$v){
                    $this->apiReturn(303,'lack '.$k);
                }
            }
            $value['add_time'] = time();
            $value['expiration'] = time() + 86400*3;
            $id = bs::addProject('user',$value);
            $user_info = bs::particular('user','id,authority,add_time,phone,gender,age,expiration,id_card,location,url',$id);
            $ex_time = $user_info['expiration'] - time();
//            halt($ex_time);
            if($ex_time>=86400){
                $user_info['expiration'] = intval($ex_time / 86400);
            }elseif ($ex_time>=0){
                $user_info['expiration'] = round(($ex_time / 3600),2);
            }else{
                $user_info['expiration'] = 0;
            }
            $this->apiReturn(200,'注册成功',$user_info);
        }
    }

    /**
     *得到openid
     * @param
     * @return
     */
    public function getCode(){
        $js_code = I('js_code');
        if(!$js_code){
            $this->apiReturn(303,'lack js_code');
        }
        $param = ['appid'=>config('appid'),'secret'=>config('secret'),'js_code'=>$js_code,'grant_type'=>'authorization_code',];
        postCurl($param,'https://api.weixin.qq.com/sns/jscode2session');
    }

    /**
     *得到手机号码
     * @param
     * @return
     */
    public function getPhone(){
        include '../extend/decode_wx_phone/wxBizDataCrypt.php';
        $value = I('sessionKey|encryptedData|iv');
        foreach ($value as $k=>$v){
            if(!$v){
                $this->apiReturn(303,'lack '.$k);
            }
        }
        $pc = new \WXBizDataCrypt(config('appid'), $value['sessionKey']);
        $errCode = $pc->decryptData($value['encryptedData'], $value['iv'], $data );

        if ($errCode == 0) {
            print($data);
        } else {
            print($errCode);
        }
    }

    /**
     *用户充值订单生成
     * @param
     * @return
     */
    public function payTheFees(){
        $value = I('open_id|pay_id');
        foreach ($value as $k=>$v){
            if(!$v){
                $this->apiReturn(303,'lack '.$k);
            }
        }
        $user_id = bs::getKey('user',['open_id'=>$value['open_id']],'id');
        $pay_template = bs::particular('pay_template','day_key,money',$value['pay_id']);
        if(!$user_id){
            $this->apiReturn(303,'没有这个用户');
        }else{
            //调用支付接口
            $wx_pay = new Paywx();
            $order_sn = orderNumber();//生成订单编号
            $price = floatval($pay_template['money'])*100;//价格计算
            $status_key = $wx_pay->weixinPayDataW($price,$value['open_id'],$order_sn);
            //$this->apiReturn(200,'',$status_key);//调试,前端返回值
            $value_key = ['user_id'=>$user_id,'pay_id'=>$value['pay_id'],'add_time'=>$status_key['timeStamp'],'order_number'=>$order_sn];
            bs::addProject('pay_user',$value_key);//插入充值记录
            $status_key['orderNumber'] = $order_sn;
            $this->apiReturn(200,'',$status_key);//调试,前端返回值
        }
    }


    /**
     * 支付回调
     * @param stirng
     * @return string
     */
    public function setFees(){
        $value = I('open_id|order_number');
        foreach ($value as $k=>$v){
            if(!$v){
                $this->apiReturn(303,'lack '.$k);
            }
        }
        $user_info = bs::particulars('user','id,expiration',['open_id'=>$value['open_id']]);//使用openid换取用户id
        $user_id = $user_info['id'];
        $pay_id = bs::particulars('pay_user','id,pay_id',['user_id'=>$user_id,'order_number'=>$value['order_number']]);//得到支付模板id
        $pay_user_id = $pay_id['id'];
        bs::editProject('pay_user',['id'=>$pay_user_id],['pay_status'=>1]);
        $pay_template = bs::particular('pay_template','day_key,money',$pay_id['pay_id']);//查询支付模板
        $expiration = $user_info['expiration'];//得到用户过期值
        if($expiration>time()){//判断用户是否已经过期，过期从现在开始算，没有过期就从要过期时间开始算
            $key_value = $expiration + $pay_template['day_key'];
            bs::editProject('user',['id'=>$user_id],['expiration'=>$key_value]);
        }else{
            $key_value = time() + $pay_template['day_key'];
            bs::editProject('user',['id'=>$user_id],['expiration'=>$key_value]);
        }
        return 'success';
    }

    /**
     *查看自己的充值记录
     * @param
     * @return
     */
    public function getLookPay(){
        $open_id = input('open_id');
        if(!$open_id){
            $this->apiReturn(303,'lack open_id');
        }
        $user_id = bs::getKey('user',['open_id'=>$open_id],'id');//获得用户id
        $data =us::getOrder(['id'=>$user_id,'pay_user.pay_status'=>1]);
        foreach ($data as $k=>$v){
            $data[$k]['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
        }
        $this->apiReturn(200,'',$data);
    }
	// 调起退款



}